//		点击事件
$("#dvcTree").click(function() {
	if($(".dvcGroupTree").css("display") == 'block') {
		$(".dvcGroupTree").hide();
	} else {
		$(".dvcGroupTree").show();
	}
})
//离开事件
$(".dvcGroupTree").mouseleave(function() {
	$(".dvcGroupTree").hide()
});

$.ajax({
	url:'orgListTree',
	type: 'POST',
	data: {
		companyId: $.Cookie("companyId"),
		session: $.Cookie("session"),
		userId: $.Cookie("userId"),
	},
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					beforeClick: beforeClick

				}
			};

			var zNodes = data.result

			var log, className = "dark";

			function beforeClick(treeId, treeNode, clickFlag) {
				$("#dvcTree").attr("value", treeNode.name); //填充内容
				$("#dvcTree").attr("name", treeNode.treeCode); //填充内容
				$(".dvcGroupTree").hide();
			}

			$(document).ready(function() {
				$.fn.zTree.init($("#dvcGroupTree"), setting, zNodes);
			});
		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';

		}
	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！")
		top.location.href = 'index.jsp';

	}
});

$.ajax({
	url:'paramTypeList',
	type: 'POST',
	data: {
		session: $.Cookie("session"),
		userId: $.Cookie("userId"),
		companyId: $.Cookie("companyId"),
		index: 0,
		page: 10

	},
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			$.each(data.result.paramTypeList, function(i, value) {
				if(data.result.paramTypeList[i].type == "ORG_TYPE") {

				} else {
					error_tpl = '<option value="' + data.result.paramTypeList[i].id + '">' + data.result.paramTypeList[i].paramValue + '</option>"'
					$("#pib").append(error_tpl)
				}

			});
		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';

		} else {
			alert("请重试！")
		}

	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！")
		top.location.href = 'index.jsp';

	}
});

var param = {
	companyId: $.Cookie("companyId"),
	userId: $.Cookie("userId"),
	session: $.Cookie("session")
}
$.ajax({
	url: 'dvcGroupList',
	type: 'POST',
	data: param,
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			$.each(data.result, function(i, value) {
				var error_tpl = '<input name="checkbox2" onclick="lista(' + "this" + ')" id="' + value.dvcGroupId + '" type="checkbox" value="' + value.dvcGroupId + '" /><div>' + value.dvcGroupName + ' </div>'
				$(".dvcGroupList").append(error_tpl)
			});

		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';

		}

	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！")
		top.location.href = 'index.jsp';

	}
});
var somearray = [];

function removeByValue(arr, val) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
}

function lista(data) {
	var tickBlock = document.getElementById(data.id);
	if(tickBlock.checked == true) {
		removeByValue(somearray, data.value);
		somearray.push(data.value);

	} else if(tickBlock.checked == false) {
		removeByValue(somearray, data.value);
	}
}

function backDevice() {
	window.location.href = 'device.jsp';

}

$.ajax({
	url: 'dvcList',
	type: 'POST',
	data: {
		companyId: $.Cookie("companyId"),
		session: $.Cookie("session"),
		userId: $.Cookie("userId"),
		orgId: 1,
		index: 0,
		page: 10
	},
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			var tickaId = sessionStorage.getItem("tickaId");
			arr = data.result.dvcList;
			for(var i = 0; i < arr.length; i++) {
				if(arr[i].id == tickaId) {
					$("#dvcTree").attr("name", arr[i].orgTreeId);
					$("#dvcId").attr("name", arr[i].id);
					$("#dvcTree").attr("value", arr[i].orgName);
					$("#pid").val(arr[i].status);
					$("#pib").val(arr[i].dvcType);
					$("#dvcId").val(arr[i].dvcId);
					$("#pid option:selected").val(arr[i].status);
					$("#remark").val(arr[i].remark)
					var dvcid = arr[i].dvcGroupId;
					for(var i = 0; i < dvcid.length; i++) {
						if(dvcid[i] == ",") {} else {
							somearray.push(dvcid[i]);
							g = document.getElementById(dvcid[i]);
							g.checked = "true";
						}
					}

				}
			}

		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';

		}

	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！")
		top.location.href = 'index.jsp';
	}
});

function submitData() {
	somearray = somearray.join(",");
	var param = {
		orgTreeId: $("#dvcTree").attr("name"),
		orgName: $("#dvcTree").val(),
		dvcGroupId: somearray,
		dvcType: $("#pib option:selected").val(),
		status: $("#pid option:selected").val(),
		remark: $("#remark ").val(),
		session: $.Cookie("session"),
		userId: $.Cookie("userId"),
		flag: "0",
		id: $("#dvcId").attr("name"),
	}
	$.ajax({
		url:'updateDeviceInfo',
		type: 'POST',
		data: param,
		dataType: 'json',
		success: function(data) {
			if(data.errorCode == 0) {
				window.location.href = 'device.jsp';

			} else {
				alert("提交超时请重试！")
				top.location.href = 'index.jsp';

			}
		},
		error: function(err) {
			alert("连接服务器失败！请重新提交！")
			top.location.href = 'index.jsp';

		}
	});

}